﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceRanking;

namespace IRProject.Model
{
    class LeafNode : Node
    {
        string term;
        List<ResultData> resultDatas;
        IFtidfAlgorithmProvider tfidfServes;
        public LeafNode(IFtidfAlgorithmProvider tfidf, string term) : base() { this.tfidfServes = tfidf; this.term = term; }

        public void Calculate(string[] docs)
        {
            string[] input = new string[docs.Length + 1];
            for (int i = 0; i < docs.Length; i++)
                input[i] = docs[i];
            input[docs.Length] = term;

            TFIDFMeasure tfidf = tfidfServes.InitializeFtidfAlgorithm();
            tfidf.LoadDocs(input);
            resultDatas = new List<ResultData>();
            for (int i = 0; i < docs.Length; i++)
            {
                ResultData res = new ResultData();
                res.DocId = i;
                res.Similarity = tfidf.GetSimilarity(i, docs.Length);
                if (res.Similarity > 0)
                    resultDatas.Add(res);
            }
        }

        public override List<ResultData> GetResult()
        {
            if (resultDatas == null)
                throw new Exception("must call Calculate first!!");
            return resultDatas;
        }
    }
}
